using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIFormationEditName : UIBaseWindow
    {
        public static UD_FormationEditName CreateUIData(int tabIndex)
        {
            var d = ClassPool<UD_FormationEditName>.Get();
            d.tabIndex = tabIndex;
            return d;
        }

        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIFormationEditName),
            layer = EUILayer.Pop,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIFormationEditName
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton btnCancel { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public IQIGame.Onigao.Framework.ExInputField inputName { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textTip { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnCancel", out var __tbv0);
                this.btnCancel = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv1);
                this.btnConfirm = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExInputField>("inputName", out var __tbv2);
                this.inputName = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textTip", out var __tbv3);
                this.textTip = __tbv3;
            }

            #endregion ObjectBinding Generate 
        }

        #endregion Template Generate,don't modify

        #region fields & properties
        FormationData formationData = null;
        private int _tabIndex;
        string oldName = null;

        protected UIB_UIFormationEditName ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIFormationEditName();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.ui.btnCancel.onClick.AddListener(this.OnClickBtnCancel);
            this.ui.btnConfirm.onClick.AddListener(this.OnClickBtnConfirm);
            this.ShowErrorTip("");
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {

        }

        protected override void OnShow(UIBaseData data)
        {
            var d = (UD_FormationEditName)data;
            this._tabIndex = d.tabIndex;
            this.formationData = FormationModule.Instance.GetFormationByIndex(_tabIndex);
            this.oldName = FormationModule.Instance.GetFormationName(formationData);
            this.ui.inputName.text = this.oldName;
        }

        protected override UIBaseData OnGetLeaveData()
        {
            return UIFormationEditName.CreateUIData(_tabIndex);
        }

        private void ShowErrorTip(string tip)
        {
            this.ui.textTip.text = tip;
        }

        void OnClickBtnCancel()
        {
            this.Close();
        }

        void OnClickBtnConfirm()
        {
            string str = this.ui.inputName.text;
            if (string.Equals(this.oldName, str))
            {
                this.Close();
                return;
            }

            string limitStr = APIUIFormation.GetStrLimitFormationInputName(str);
            if (!string.IsNullOrEmpty(limitStr))
            {
                this.ShowErrorTip(limitStr);
                return;
            }
            this.SendSetFormName(str).Forget();
        }

        async UniTaskVoid SendSetFormName(string name)
        {
            var result = await FormationModule.NetCore.CS_setFormName(this.formationData.formId, name);
            if (result.IsSuccess)
            {
                this.Close();
            }
            else
            {
                var cfgOne = TableCenter.errorCode.Get(result.code);
                if (cfgOne != null)
                    this.ShowErrorTip(cfgOne.Describe);
            }
        }
    }
}
